/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test.shanika;

import com.googlecode.javacpp.Loader;
import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.FrameGrabber;
import com.googlecode.javacv.FrameGrabber.Exception;
import com.googlecode.javacv.VideoInputFrameGrabber;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import static com.googlecode.javacv.cpp.opencv_core.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author HOME
 */
public class calLength {

    static CvScalar BLUE_MIN = cvScalar(10, 0, 0, 0);
    static CvScalar BLUE_MAX = cvScalar(255, 15, 50, 0);
    int FRAME_COUNT = 100;
    int MAX_LENGTH = 16;
    CanvasFrame canvas = new CanvasFrame("0");
    CanvasFrame canvas1 = new CanvasFrame("1");
    CanvasFrame canvas2 = new CanvasFrame("2");
    FrameGrabber grabber = new VideoInputFrameGrabber(0);
    CvSeq contours = new CvSeq(null);
    CvMemStorage storage = CvMemStorage.create();
    int[] numSqure = new int[FRAME_COUNT];

    public static void main(String[] args) {

        calLength cl = new calLength();
        cl.getThresholdImage1();
    }

    public void getThresholdImage1() {

        try {
            grabber.start();
            Thread.sleep(1000);

        //    for (int i = 0; i < FRAME_COUNT; i++) {
            while(true){

                IplImage orgImg = grabber.grab();
                IplImage imgThreshold = cvCreateImage(cvGetSize(orgImg), 8, 1);
                IplImage supp_img = cvCreateImage(cvGetSize(orgImg), 8, 1);
                cvInRangeS(orgImg, BLUE_MIN, BLUE_MAX, imgThreshold);
                canvas.showImage(imgThreshold);

                cvSmooth(imgThreshold, imgThreshold, CV_MEDIAN, 5);
                canvas1.showImage(imgThreshold);

                IplConvKernel se = cvCreateStructuringElementEx(3, 3, 1, 1, CV_SHAPE_RECT, null); // Struncturing Element
                IplConvKernel se1 = cvCreateStructuringElementEx(7, 7, 4, 4, CV_SHAPE_RECT, null); // Struncturing Element

                cvMorphologyEx(imgThreshold, imgThreshold, supp_img, se, CV_MOP_CLOSE, 2);
                cvMorphologyEx(imgThreshold, imgThreshold, supp_img, se1, CV_MOP_OPEN, 1);

                canvas2.showImage(imgThreshold);

                int cont = cvFindContours(imgThreshold, storage, contours, Loader.sizeof(CvContour.class), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
           //     numSqure[i] = cont;

                cvClearMemStorage(storage);
                cvReleaseImage(imgThreshold);
                cvReleaseImage(supp_img);

            }
          //  int value = this.getMod(numSqure);
        //    int length = value * 2;
//            System.out.println("Value " + value);
//            System.out.println("Length= " + length);

        } catch (InterruptedException ex) {
            Logger.getLogger(calLength.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(calLength.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public int getMod(int[] src) {

        int[] cumHist = new int[MAX_LENGTH];
        int[] histogram = new int[MAX_LENGTH];

        for (int i = 0; i < cumHist.length; i++) {
            for (int j = 0; j < src.length; j++) {
                if (src[j] < i + 1) {
                    cumHist[i] = cumHist[i] + 1;
                }
            }
        }

        histogram[0] = cumHist[0];
        for (int k = 1; k < cumHist.length; k++) {
            histogram[k] = cumHist[k] - cumHist[k - 1];
        }

        for (int i = 0; i < histogram.length; i++) {
            System.out.println(i + " Histogram value= " + histogram[i]);
        }
        
        int largets = 0;
        int maxVal = 0;

        for (int i = 0; i < histogram.length; i++) {
            if (largets < histogram[i]) {
                largets = histogram[i];
                maxVal = i;
            }
        }
        return maxVal;
    }
}
